/**
 * Created by Asura on 2016/2/3.
 * 中间件的使用
 */
var http = require('http'),
    connect = require('connect');

var app = connect().use(nineToFiveCanVisit).use(filterByIp).use(go);
/**
 * 9点到5点之间才能访问
 * @param req
 * @param res
 * @param next
 */
function nineToFiveCanVisit(req, res, next) {
    var hours = new Date().getHours();
    if (hours < 9 || hours > 17) {
        res.writeHead(503, {
            'Content-Type': 'text/plain'
        });
        res.end('only in 9~17 o`clock can visit');
    } else {
        next();
    }
}
function filterByIp(req, res, next){
    var ip = req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
    console.log(ip);
    if(ip.indexOf('127.0.0.1')) {
        next();
    }else {
        res.writeHead(401, {
            'Content-Type': 'text/plain'
        });
        res.end('不好意思，您的ip被我们墙了');
    }
}
function go(req, res) {
    res.end('welcome little buddy');
}
http.Server(app).listen(3000);
